#include "bh1750_my.h"


#define LOG_TAG     "light"     // 该模块对应的标签。不定义时，默认：NO_TAG
#define LOG_LVL     LOG_LVL_DBG   // 该模块对应的日志输出级别。不定义时，默认：调试级别
#include <ulog.h>                 // 必须在 LOG_TAG 与 LOG_LVL 下面

static rt_device_t bh1750_dev = RT_NULL;
void bh1750_open(void)
{
    /* 查找设备 */
    //rt_kprintf("li_bh1750 open ! ");
    bh1750_dev = rt_device_find("li_bh1750");
    if (bh1750_dev != RT_NULL)
    {
        rt_device_open(bh1750_dev, RT_DEVICE_FLAG_RDONLY);

    }else {
        LOG_W("li_bh1750 find failed!");
    }

}
/**
 *      读取光照传感器数据
 *@param 无
 *
 *@return 读取不到返回0，读取到返回数据
 */

rt_int32_t bh1750_read_data(void)
{

    struct rt_sensor_data data;
    rt_int32_t liuming=0;
    rt_size_t res;
    if (bh1750_dev != RT_NULL)
    {
        res = rt_device_read(bh1750_dev, 0, &data, 1);
        if (res != 1)
        {
            LOG_W("bh1750 read data failed!size is %d\n", res);
            return liuming;
        }
        else
        {
            //rt_kprintf("bh1750 read data success!data:%d\n", data.data.light);
        }
        liuming=data.data.light;
        return liuming;
    }else {
        LOG_W("li_bh1750 find failed!");
    }

}


void bh1750_close(void)
{
    rt_err_t result = RT_EOK;
    if (bh1750_dev != RT_NULL)
    {
        result=rt_device_close(bh1750_dev);
        if (result != RT_EOK)
        {
            LOG_W("bh1750_close failed!\n");
        }

    }else {
        LOG_W("li_bh1750 find failed!");
    }

}


/*//光照传感器测试线程
static void guang_text_entry(void *parameter)
{
    rt_int32_t liuming=0;
    rt_int32_t liuming_max=0;
    bh1750_open();
    for(uint8_t i=0;i<30;i++)
    {

        liuming=bh1750_read_data();
        if(liuming_max<liuming)
            liuming_max=liuming;
    }
    LOG_I("bh1750 read data over!data_max>>>>%d<<<<<\n", liuming_max);
    bh1750_close();
}
//光照度传感器测试线程启动函数
static int bh1750_sample(void)
{

    rt_thread_t distance_thread;
    distance_thread = rt_thread_create("guang",
                                        guang_text_entry,
                                     RT_NULL,
                                     2048,
                                     RT_THREAD_PRIORITY_MAX / 2,
                                     20);
    if (distance_thread != RT_NULL)
    {
        rt_thread_startup(distance_thread);
    }
    LOG_I("guang_zhao_thread start!\n");
    return RT_EOK;
}

MSH_CMD_EXPORT(bh1750_sample , bh1750_txt_my);*/
